// pages/history/history.ts
import request from '../../utils/request'
// 获取应用实例
const app = getApp<IAppOption>()
Page({

    /**
     * 页面的初始数据
     */
    data: {
        globalData: {},
        visitNo: '',// 患者ID
        
        historyList: [],
        pageNum: 1,
        pageSize: 30,
        total: 0,
        isLoading: false,
    },

    // 上拉触发刷新
    getUpData(){
        let that = this;
        if(that.data.isLoading){
            return ;
        }
        wx.showLoading({
            title: '正在刷新...',
        });
        that.setData({
            isLoading: true,
            pageNum: 1,
            total: 0,
            historyList: [],
        });
        request.get('/applet/equipment/getBraceletHistoryData', { visitNo: this.data.visitNo, pageNum: this.data.pageNum, pageSize: this.data.pageSize }).then((res: any) => {
            wx.hideLoading();
            that.setData({
                isLoading: false
            });
            let data = res.data.data.data;
            let total = res.data.data.total;
            that.setData({
                historyList: data,
                total: total,
            });
        });
    },
    // 下拉触发加载
    getDownData(){
        if(this.data.isLoading){
            return ;
        }
        let that = this;
        if(this.data.pageSize * (this.data.pageNum) < this.data.total){
            wx.showLoading({
                title: '数据加载中...',
            });
            this.setData({
                isLoading: true,
            });
            let page = that.data.pageNum + 1;

            request.get('/applet/equipment/getBraceletHistoryData', { visitNo: this.data.visitNo, pageNum: page, pageSize: this.data.pageSize }).then((res: any) => {
                wx.hideLoading();
                this.setData({
                    isLoading:false
                });
    
                let data = res.data.data.data;
                let total = res.data.data.total;
                that.setData({
                    historyList: [...that.data.historyList, ...data],
                    pageNum: page,
                    total: total
                });
            })
        } else {
            wx.showToast({
                title: "没有数据了",
                icon: 'loading',
                duration: 1000
            })
        }
    },
    upper(e){
        if(e.detail.direction == "top"){
            this.getUpData()
        }
    },
    lower(e){
        if(e.detail.direction == "bottom"){
            this.getDownData()
        }
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(e: any) {
        this.setData({
            visitNo: e.visitNo
        })
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {
    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {
        this.setData({
            globalData: app.globalData,
        })
        this.getUpData();
    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    }
})